home *** CD-ROM | disk | FTP | other *** search
/ Download Now 8 / Download Now V8.iso / Program / InternetTools / ApacheWebServer1.3.6 / apache_1_3_6_win32.exe / _SETUP.1 / readdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  2.0 KB  |  81 lines

  1. #include <malloc.h>
  2. #include <string.h>
  3. #include <errno.h>
  4.  
  5. #include "readdir.h"
  6.  
  7. /**********************************************************************
  8.  * Implement dirent-style opendir/readdir/closedir on Window 95/NT
  9.  *
  10.  * Functions defined are opendir(), readdir() and closedir() with the
  11.  * same prototypes as the normal dirent.h implementation.
  12.  *
  13.  * Does not implement telldir(), seekdir(), rewinddir() or scandir(). 
  14.  * The dirent struct is compatible with Unix, except that d_ino is 
  15.  * always 1 and d_off is made up as we go along.
  16.  *
  17.  * The DIR typedef is not compatible with Unix.
  18.  **********************************************************************/
  19.  
  20. API_EXPORT(DIR *) opendir(const char *dir)
  21. {
  22.     DIR *dp;
  23.     char *filespec;
  24.     long handle;
  25.     int index;
  26.  
  27.     filespec = malloc(strlen(dir) + 2 + 1);
  28.     strcpy(filespec, dir);
  29.     index = strlen(filespec) - 1;
  30.     if (index >= 0 && (filespec[index] == '/' || filespec[index] == '\\'))
  31.         filespec[index] = '\0';
  32.     strcat(filespec, "/*");
  33.  
  34.     dp = (DIR *)malloc(sizeof(DIR));
  35.     dp->offset = 0;
  36.     dp->finished = 0;
  37.     dp->dir = strdup(dir);
  38.  
  39.     if ((handle = _findfirst(filespec, &(dp->fileinfo))) < 0) {
  40.         if (errno == ENOENT)
  41.             dp->finished = 1;
  42.         else
  43.         return NULL;
  44.     }
  45.  
  46.     dp->handle = handle;
  47.     free(filespec);
  48.  
  49.     return dp;
  50. }
  51.  
  52. API_EXPORT(struct dirent *) readdir(DIR *dp)
  53. {
  54.     if (!dp || dp->finished) return NULL;
  55.  
  56.     if (dp->offset != 0) {
  57.         if (_findnext(dp->handle, &(dp->fileinfo)) < 0) {
  58.             dp->finished = 1;
  59.             return NULL;
  60.         }
  61.     }
  62.     dp->offset++;
  63.  
  64.     strncpy(dp->dent.d_name, dp->fileinfo.name, _MAX_FNAME);
  65.     dp->dent.d_ino = 1;
  66.     dp->dent.d_reclen = strlen(dp->dent.d_name);
  67.     dp->dent.d_off = dp->offset;
  68.  
  69.     return &(dp->dent);
  70. }
  71.  
  72. API_EXPORT(int) closedir(DIR *dp)
  73. {
  74.     if (!dp) return 0;
  75.     _findclose(dp->handle);
  76.     if (dp->dir) free(dp->dir);
  77.     if (dp) free(dp);
  78.  
  79.     return 0;
  80. }
  81.